package com.xzy.seckill.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzy.seckill.common.result.R;
import com.xzy.seckill.pojo.entity.Goods;
import com.xzy.seckill.pojo.vo.GoodDetailVO;
import com.xzy.seckill.pojo.vo.GoodListVO;
import com.xzy.seckill.pojo.vo.SeckillGoodDetailVO;
import com.xzy.seckill.service.GoodsService;
import com.xzy.seckill.service.SeckillGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xzy
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/admin/goods")
@Api(tags = "商品展示相关接口")
public class GoodsController {


    @Autowired
    GoodsService goodsService;

    @Autowired
    SeckillGoodsService seckillGoodsService;


    @GetMapping("/test")
    public R test(){
        return R.ok();
    }

    //正价商品列表（这里不是重点，就简略写）
    @ApiOperation(value = "正价商品列表" )
    @GetMapping("/list/{page}/{limit}")
    public R list(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                  @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit
                 ) {


        Page<GoodListVO> goodListVOPage = new Page<>(page, limit);

        IPage<GoodListVO> list = goodsService.list(goodListVOPage);

        return R.ok().message("获取成功").data("page",list);
    }





    //正价商品详情(这里不是重点，就简略写）
    @ApiOperation(value = "正价商品详情" )
    @GetMapping("/detail/{goodId}")
    public R detail(@ApiParam(value = "商品id", required = true) @PathVariable int goodId
    ) {
        Goods good = goodsService.getById(goodId);
        GoodDetailVO goodDetailVO = new GoodDetailVO();
        BeanUtils.copyProperties(good,goodDetailVO);

        return R.ok().message("获取成功").data("good",goodDetailVO);
    }

    //秒杀商品详情(这里不是重点，就简略写）
    @ApiOperation(value = "秒杀商品详情" )
    @GetMapping("/seckill/{goodId}")
    public R seckillDetail(@ApiParam(value = "商品id", required = true) @PathVariable int goodId
    ) {
        //这里的id为表中goodsid
        SeckillGoodDetailVO seckillGood = seckillGoodsService.getByid(goodId);

        return R.ok().message("获取成功").data("good",seckillGood);
    }

    //秒杀商品列表（这里不是重点，就简略写）
    @ApiOperation(value = "秒杀商品列表" )
    @GetMapping("/seckillList/{page}/{limit}")
    public R seckillList(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                  @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit
    ) {


        Page<SeckillGoodDetailVO> goodListVOPage = new Page<>(page, limit);

        IPage<SeckillGoodDetailVO> list = seckillGoodsService.list(goodListVOPage);

        return R.ok().message("获取成功").data("page",list);
    }

}

